<#import "../template.ftl" as t/>
<@t.html ; part>
    <#if part="head">
    </#if>
    <#if part="body">
        <div class="panel">
            <div class="panel-heading">
                <h3 class="panel-title">设置数据权限</h3>
            </div>
            <form class="panel-body form-horizontal form-padding">
                <#if cityList?? && cityList?size &gt; 0>
                    <#list cityList as city>
                        <div class="form-group">
                            <div class="col-md-12">
                                <input id="form-checkbox" value="${city.id}" pData="${city.id}" type="checkbox" name="cityCheckbox" onclick="chooseArea(${city.id}, this)" <#if cityIdList?? && cityIdList?seqContains(city.id)>checked</#if> >
                                <label for="demo-form-radio">${city.name}</label>
                            </div>
                        </div>
                        <#if cityMap?? && cityMap[(city.id)?c]??>
                            <div class="form-group pad-ver">
                                <div class="col-md-12">
                                    <#list cityMap[(city.id)?c] as area>
                                        <div class="checkbox col-md-3">
                                            <input id="form-checkbox" cData="${area.parentId}" value="${area.id}" type="checkbox" name="cityCheckbox" onclick="chooseCity(${area.parentId}, this)" <#if cityIdList?? && cityIdList?seqContains(area.id)>checked</#if>>
                                            <label for="demo-form-radio">${area.name}</label>
                                        </div>
                                    </#list>
                                </div>
                            </div>
                        </#if>
                    </#list>
                </#if>
            </form>
            <div class="panel-footer text-left">
                <button class="btn btn-success" type="button" onclick="saveSetting(${userId})">提交</button>
            </div>
        </div>

    </#if>
    <#if part="modal">
    </#if>

    <#if part="foot_script">
        <script type="text/javascript">
            function saveSetting(userId) {
                if (confirm("确定保存数据权限设置吗？")){
                    var cityIds = new Array();
                    $("input:checkbox[name='cityCheckbox']:checked").each(function () {
                        cityIds.push($(this).val());
                    });
                    $.post("<@spring.url "/manage/dataSetting/saveSetting"/>",{
                        userId:userId, cityIds:cityIds}, function (data) {
                        alert(data.message);
                        if (data.code == 1) {
                            window.location.reload();
                        }
                    });
                }
            }

            function chooseArea(cityId, obj) {
                if ($(obj).is(':checked')) {
                    $("input:checkbox[name='cityCheckbox'][cData='"+cityId+"']").each(function () {
                        $(this).prop("checked", true);
                    });
                }  else {
                    $("input:checkbox[name='cityCheckbox'][cData='"+cityId+"']").each(function () {
                        $(this).prop("checked", false);
                    })
                }
            }

            function chooseCity(cityId, obj) {
                if ($(obj).is(':checked')) {
                    var allCheckbox = $("input:checkbox[name='cityCheckbox'][cData='"+cityId+"']").length;
                    var currentCheckbox = $("input:checkbox[name='cityCheckbox'][cData='"+cityId+"']:checked").length;
                    if (allCheckbox!= undefined && currentCheckbox != undefined && allCheckbox==currentCheckbox) {
                        $("input:checkbox[name='cityCheckbox'][pData='"+cityId+"']").each(function () {
                            $(this).prop("checked", true);
                        });
                    }
                } else {
                    $("input:checkbox[name='cityCheckbox'][pData='"+cityId+"']").each(function () {
                        $(this).prop("checked", false);
                    })
                }
            }
        </script>

    </#if>
</@t.html>